package com.hdw.job.batch.tasklet;

import com.hdw.common.base.BaseException;
import com.hdw.common.db.DynamicDataSource;
import com.hdw.common.db.HikariDataSourceExt;
import com.hdw.common.util.DBUtil;
import com.hdw.common.util.DateUtil;
import com.hdw.common.util.EtlUtil;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component("delSQLExecTasklet")
@StepScope
public class DelSQLExecTasklet implements Tasklet {
    @Value("#{jobParameters[idDbTar]}")
    String idDbTar;
    @Value("#{jobParameters[dBegin]}")
    String startDate;
    @Value("#{jobParameters[dEnd]}")
    String endDate;
    @Value("#{jobParameters[delSql]}")
    String delSql;

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        HikariDataSourceExt dwDataSource = DynamicDataSource.getHikariDataSourceById(idDbTar);
        if (dwDataSource == null) throw new BaseException(String.format("数据源获取失败！"));

        if (StringUtils.isEmpty(this.getSQL())) return RepeatStatus.FINISHED;

        if (StringUtils.isEmpty(this.getSQL()) && this.getSQL().trim().length() <= 6) return RepeatStatus.FINISHED;

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        String sqls = null;
        try {
            connection = dwDataSource.getConnection();
            connection.setAutoCommit(true);
            Date start = DateUtil.toDateByFormat(startDate, "yyyyMMdd HH:mm:ss");
            Date end = DateUtil.toDateByFormat(endDate, "yyyyMMdd HH:mm:ss");
            sqls = EtlUtil.buildSql(this.getSQL(), start, end, dwDataSource.getEuTp());
            List<String> sqlList = new ArrayList<>();
            for (String sql : sqls.split(";")) {
                if (!StringUtils.isEmpty(sql) && sql.trim().length() > 6) {
                    sqlList.add(sql);
                }
            }
            Statement statement = connection.createStatement();

            for (String beforeSql : sqlList) {
                statement.execute(beforeSql);
            }
        } catch (Exception e) {
            e.printStackTrace();
            BaseException baseException = new BaseException("执行SQL错误! SQL语句为:" + sqls);
            throw baseException;
        } finally {
            DBUtil.closeDBResources(null, preparedStatement, connection);
        }

        return RepeatStatus.FINISHED;
    }


    protected String getSQL() {
        return this.delSql;
    }
}